<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class CustomerController extends AdminbaseController{

	
	//客户列表
	public function index(){
		$Customer      = M('Customer');
		$Recharge_log  = M('recharge_log');
		$System_config = D('Common/SystemConfig');
		if(!empty($_GET['customer_name']) || !empty($_POST['customer_name'])) $where['customer_name'] = $_GET['customer_name'] ? I('get.customer_name') : I('post.customer_name');
        if(!empty($_GET['mobile'])       || !empty($_POST['mobile'])) $where['mobile'] = $_GET['mobile'] ? I('get.mobile'): I('post.mobile'); 
        if(isset($_POST['status']) && $_POST['status'] >=0) $where['status'] = $_POST['status']; 
        
        $this->assign('customer_status',isset($_POST['status']) ? $_POST['status'] : '-1');
        $this->assign('customer_name',$where['customer_name']);
        $this->assign('mobile',$where['mobile']);
       
		$customer_list = $Customer->where($where)->order('id desc')->select();
        foreach ($customer_list as $key => $val) {
        	$last_recharge_time = $Recharge_log->where('customer_mobile ='.$val['mobile'])->getField('max(recharge_time)');
        	$customer_list[$key]['last_recharge_time'] = $last_recharge_time ? date('Y-m-d H:i',$last_recharge_time) : '---';
        }

		$this->assign('list',$customer_list);
		$this->assign('customer_status_list',$System_config->getSystemConfigItem('customer_status'));
		$this->display();
	}
	
	public function add(){
		$System_config = D('Common/SystemConfig');
		$this->assign('customer_status_list',$System_config->getSystemConfigItem('customer_status'));

		//会员类型
		$customer_type_list = M('Customer_type')->order('id asc')->select();
		$this->assign('customer_type_list',$customer_type_list);
		$this->display();
	}

	public function customer_post(){
		
		$Customer = M('Customer');
		$customer_info['customer_name']    = I('post.customer_name');
		$customer_info['sex']              = I('post.sex');
		$customer_info['status']           = 0;
		$customer_info['money']            = 0;
        $customer_info['mobile']           = I('post.mobile');
        $customer_info['create_time']      = date('Y-m-d H:i:s',time());
        $customer_info['customer_type_id'] = I('post.customer_type_id');
        $customer_info['active_time']      = strtotime(I('post.active_time'));
        $customer_info['expiration_time']  = strtotime(I('expiration_time')); 
        
        //$customer_type_info = M('Customer_type')->where('id = '.$customer_info['customer_type_id'])->find();
        
        // if($customer_type_info['type'] == 'B' && $customer_info['active_time'] <= time()){
        // 	$customer_info['money'] -= $customer_type_info['amount']; 
        // 	$customer_info['status'] = 2; //欠费
        // 	$remarks                 = $customer_type_info['memo'].'，单价￥'.$customer_type_info['amount'];
        // }
 
        if($Customer->where('mobile='.$customer_info['mobile'])->find()){
            $this->error("添加失败，该客户已经存在！");
        }else{
			if($result = $Customer->add($customer_info)){
				// if($customer_info['status'] == 2){
                // $this->addChargeLog($result,$customer_info,$remarks);
				// }

				addActionLog('customer_add',$result);

				$this->success("添加成功！", U("Customer/index"));
			}else{
				$this->error("添加失败！");
			}
	    }
	}

	//加入消费logo
	// private function addChargeLog($result,$customer_info,$remarks){
 //        $charge_info['customer_name']   = $customer_info['customer_name'];
 //        $charge_info['customer_id']     = $result;
 //        $charge_info['customer_mobile'] = $customer_info['mobile'];
 //        $charge_info['charge_money']    = abs($customer_info['money']);
 //        $charge_info['remarks']   = $remarks;
 //        $charge_info['charge_time']   = time();
 //        M('charge_log')->add($charge_info);
 //    }

	public function edit(){
		if(IS_POST){
			$where['id']                    =(int)$_POST['id'];
			$customer_info['customer_name'] = I('post.customer_name');
		    $customer_info['sex']           = I('post.sex');
		    $customer_info['status']        = I('post.status');
		    //$customer_info['money']         = I('post.money');
            //$customer_info['mobile']        = I('post.mobile');
            $customer_info['update_time']   = date('Y-m-d H:i:s',time());
            $customer_info['create_time']   = date('Y-m-d H:i:s',time());
            $customer_info['customer_type_id'] = I('post.customer_type_id');

            $result = M('Customer')->where($where)->save($customer_info);
			if($result === 0 || $result){
				addActionLog('customer_edit',$where['id']);
				$this->success("修改成功！", U("Customer/index"));
			}else{
				$this->error("修改失败！");
			}
		}
		$where['id']   = (int)$_GET['id'];
		$customer_info = M('Customer')->where($where)->find();
		
		$System_config = D('Common/SystemConfig');
		$this->assign('customer_status_list',$System_config->getSystemConfigItem('customer_status'));


		//会员类型
		$customer_type_list = M('Customer_type')->order('id asc')->select();
		$this->assign('customer_type_list',$customer_type_list);
    
        //$customer_info['active_time'] = date('Y-m-d',$customer_info['active_time']);    dump($customer_info);
		$this->assign('customer_info',$customer_info);
		$this->display();
	}

	//客户状态激活
	public function customer_active(){
		$Customer         = M('Customer');
		$id               = (int)$_GET['id'];

		$update['active_time'] = strtotime(date('Y-m-d',time()));
		$update['status']      = 1;
		
		$customer_type_id      = $Customer->where('id = '.$id)->getField('customer_type_id');
		$customer_type         = M('Customer_type')->where('id = '.$customer_type_id)->field('type');

		$update['expiration_time'] = $customer_type == 'B' ? $update['active_time']+32400000 : $update['active_time']+31536000;

		$data['status']        = $Customer->where('id = '.$id)->save($update) ? 1 : 0;

		$this->ajaxReturn($data);
	}

	//检验客户是否村子
	public function isCustomer(){
		$where['mobile'] = I('post.mobile');
		if($result = M("customer")->where($where)->field('customer_name,money')->find()){
            $data['status']        = 1;
            $data['money']         = $result['money'];
            $data['name'] = $result['customer_name'];
		}else{
			$data['status']        = 0;
			$data['info']          = '不存在该客户信息！';
		}
		$this->ajaxReturn($data);
	}

	//客户概要
	public function customer_profiles(){
		$Recharge_log = M('Recharge_log');
		$Charge_log = M('charge_log');
		$where['mobile'] = I('get.customer_mobile');
		//客户的基本信息
		$customer_info = M('customer')->where($where)->find();
        //最大充值金额,与最后充值时间
        $recharge_info = $Recharge_log->where('customer_mobile ='.$where['mobile'])->field("max(recharge_money) as big_recharge_money,sum(recharge_money) as sum_recharge_money,max(recharge_time) as last_recharge_time")->find();
        //最大消费金额,与最后消费时间
        $charge_info   = $Charge_log->where('customer_mobile ='.$where['mobile'])->field("sum(charge_money) as sum_charge_money,max(charge_time) as last_charge_time")->find();
        
        $finance_info['big_recharge_money'] = (int)$recharge_info['big_recharge_money'];
        $finance_info['sum_recharge_money'] = (int)$recharge_info['sum_recharge_money'];
        $finance_info['last_recharge_time'] = $recharge_info['last_recharge_time'] ? date('Y-m-d H:i',$recharge_info['last_recharge_time']) : '---';

        $finance_info['sum_charge_money'] = (int)$charge_info['sum_charge_money'];
        $finance_info['last_charge_time'] = $charge_info['last_charge_time'] ? date('Y-m-d H:i',$charge_info['last_charge_time']) : '---';

        //该客户充值记录
        $this->assign('recharge_list',$Recharge_log->where('customer_mobile ='.$where['mobile'])->limit(0,5)->order('id desc')->select());

        //该客户消费记录
         $this->assign('charge_list',$Charge_log->where('customer_mobile ='.$where['mobile'])->limit(0,5)->order('id desc')->select());
        $this->assign('finance_info',$finance_info);
		$this->assign('customer_info',$customer_info);
		
	 	$this->display();
	} 
}